package com.think.archive.controller;

import com.alibaba.druid.util.HttpClientUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiSnsGetuserinfoBycodeRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiSnsGetuserinfoBycodeResponse;
import com.taobao.api.ApiException;
import com.think.archive.util.HttpClientUtil;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

@Controller
public class IndexController {

    @Value("${ddApp.appId}")
    private String appId;

    @Value("${ddApp.appSecret}")
    private String appSecret;

    @RequestMapping("login")
    public String login(){
        return "user/login";
    }

    @RequestMapping("ddlogin")
    public String ddLogin(@RequestParam(required = false,value = "code") String code) {
        DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/sns/getuserinfo_bycode");
        OapiSnsGetuserinfoBycodeRequest req = new OapiSnsGetuserinfoBycodeRequest();
        req.setTmpAuthCode(code);
        try {
            OapiSnsGetuserinfoBycodeResponse response = client.execute(req, appId, appSecret);
            System.out.println(response.getBody());
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return "user/login";
    }
}
